package com.jaryle.wenxin.util.menu;

import com.alibaba.fastjson.JSONObject;
import com.jaryle.wenxin.bean.menu.BaseButton;
import com.jaryle.wenxin.bean.menu.BaseMenu;
import com.jaryle.wenxin.bean.menu.ClickButton;
import com.jaryle.wenxin.bean.menu.ViewButton;
import com.jaryle.wenxin.config.GlobalConstant;
import com.jaryle.wenxin.util.HttpClientUtil;
import com.jaryle.wenxin.util.WeiXinUtil;

/**
 * 描述:
 * 自定义菜单工具类
 *
 * @author jaryle
 * @version V1.0
 * @create 2018-12-17
 */
public class MenuUtil {

    /**
     * 创建菜单
     * @param accessToken
     * @param Menu 菜单json格式字符串
     * @return
     */
    public static JSONObject createMenu(String accessToken,String Menu){
//        int result = Integer.MIN_VALUE; //表示Integer表示的最小值-231
        JSONObject jsonObject = new JSONObject();
        String url = GlobalConstant.WEIXIN_MENU_URL.replaceAll("ACCESS_TOKEN", accessToken);
        String rs = HttpClientUtil.HttpPost(url, Menu);
        JSONObject json = JSONObject.parseObject(rs);

        //从返回的数据包中取数据{"errcode":0,"errmsg":"ok"}
        jsonObject.put("errcode", json.getString("errcode"));
        jsonObject.put("errmsg", json.getString("errmsg"));
        return jsonObject;

    }

    /**
     * 组装菜单
     * @return
     */
    public static String initMenu(){
        String result = "";
        //创建点击一级菜单
        ClickButton button11 = new ClickButton();
        button11.setName("往期活动");
        button11.setKey("11");
        button11.setType("click");

        //创建跳转型一级菜单
        ViewButton button21 = new ViewButton();
        button21.setName("百度一下");
        button21.setType("view");
        button21.setUrl("https://www.baidu.com");

        //创建其他类型的菜单与click型用法一致
        ClickButton button31 = new ClickButton();
        button31.setName("拍照发图");
        button31.setType("pic_photo_or_album");
        button31.setKey("31");

        ClickButton button32 = new ClickButton();
        button32.setName("发送位置");
        button32.setKey("32");
        button32.setType("location_select");

        //封装到一级菜单
        BaseButton button = new BaseButton();
        button.setName("菜单");
        button.setType("click");
        button.setSubButton(new BaseButton[]{button31,button32});

        //封装菜单
        BaseMenu menu = new BaseMenu();
        menu.setBaseButton(new BaseButton[]{button11,button21,button});
        return JSONObject.toJSONString(menu);
    }
}
